package com.gt.aep.web.systemManage;

import com.alibaba.fastjson.JSONObject;
import com.gt.aep.base.entity.systemManage.SysPermission;
import com.gt.aep.base.entity.systemManage.SysPermissionSysRole;
import com.gt.aep.base.entity.util.Result;
import com.gt.aep.base.service.systemManage.SysPermissionSysRoleService;
import com.gt.aep.base.util.EncryptUtil;
import org.apache.commons.collections.map.HashedMap;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.UUID;

@RestController
@RequestMapping("sysPermsiionSysRole")
public class SysPermissionSysRoleController {

    @Autowired
    SysPermissionSysRoleService sysPermissionSysRoleService;

    //删除
    // 1、删除权限时同时删除 SysPermissionController
    // 2、删除系统角色时同时删除 SysRoleController


    // TODO 新增权限-系统角色关系

    //修改
    @RequestMapping("updatePermissionSysRole")
    public Map<String, Object> updatePermissionSysRole(String data,String encryptAesKey){
        //解密前端发送过来的数据
        String jsPublicKey = null;
        Map<String, Object> decData = EncryptUtil.decode(data,encryptAesKey);
        String jsAesKey = (String) decData.get("jsAesKey");
        String decrypt = (String) decData.get("data");
        //将String类型数据转化为对象
        JSONObject dataObject = new JSONObject(JSONObject.parseObject(decrypt));
        //根据前端的字段获取相对应的属性
        jsPublicKey = (String) dataObject.get("jsPublicKey");
        String roleId = (String) dataObject.get("roleId");
        String[] pids = (String[]) dataObject.get("pids");
        //修改思路：
        // 1、根据删除所有系统角色ID为 roleId 的记录
        // 2、新增权限-系统角色关系记录
        int i = sysPermissionSysRoleService.deleteBySysRid(roleId);
        if (i>0){
            int l = 0;
            for(int j = 0; j < pids.length; j++){
                SysPermissionSysRole sysPermissionSysRole = new SysPermissionSysRole();
                sysPermissionSysRole.setId(UUID.randomUUID().toString());
                sysPermissionSysRole.setSysRid(roleId);
                sysPermissionSysRole.setPid(pids[j]);
                int k = sysPermissionSysRoleService.insert(sysPermissionSysRole);
                if (k>0){
                    l++;
                }
            }
            if (l == pids.length){
                return Result.success("修改成功");
            }else{
                return Result.error("修改失败");
            }
        }else{
            return Result.error("清除旧关系失败");
        }
    }

    /**
     * 根据身份角色ID获取权限列表
     */
    @RequestMapping("getPermissionListBySysRoleId")
    public Map<String, Object> getPermissionListBySysRoleId(String data, String encryptAesKey){
        //解密前端发送过来的数据
        String jsPublicKey = null;
        Map<String, Object> decData = EncryptUtil.decode(data,encryptAesKey);
        String jsAesKey = (String) decData.get("jsAesKey");
        String decrypt = (String) decData.get("data");
        //将String类型数据转化为对象
        JSONObject dataObject = new JSONObject(JSONObject.parseObject(decrypt));
        //根据前端的字段获取相对应的属性
        jsPublicKey = (String) dataObject.get("jsPublicKey");
        String sysRoleId = (String) dataObject.get("sysRoleId");
        List<SysPermission> sysPermissionList = sysPermissionSysRoleService.getListBySysRoleId(sysRoleId);
        int count = sysPermissionSysRoleService.getCountBySysRoleId(sysRoleId);
        List<Map<String, Object>> dataList = new LinkedList<>();
        Map<String, Object> model = new HashedMap();
        model.put("data", sysPermissionList);
        model.put("count",count);
        dataList.add(model);
        return EncryptUtil.encryptData(dataList,jsPublicKey);
    }

}
